import { remove, getList, save } from '@/api/maint/accountPwd'
import { getListByDict as getUserStates} from '@/api/maint/dict'
// import {  getList as getMsgReminder } from '@/api/maint/msgreminder'
import {  getall as getWeChatAll } from '@/api/maint/weChat'
import {  getall as getDingDingAll } from '@/api/maint/dingDing'
import permission from '@/directive/permission/index.js'

export default {
  directives: { permission },
  data() {
    return {
      formVisible: false,
      formTitle: '添加账号信息',
      deptList: [],
      roleList: [],
     
      isAdd: true,
      checkedPermissionKeys: [],
      permissons: [],
      defaultProps: {
        id: 'id',
        label: 'name',
        children: 'children'
      },
      permissonVisible: false,
      form: {
        id: '',
        name: '',
        websiteUrl: '',
        accountName: '',
        password: '',
        currentState: '',
        description: '',
        startTime:'',
        endTime:'',
        reminderTime:'',
        msgReminderType:'',
        reminderTemplate:'',
        isReminder:false,
        disabledPwd:true
      },
      rules: {
        name: [
          { required: true, message: '请输入工作项', trigger: 'blur' },
        ],
        websiteUrl: [
          { required: true, message: '请输入网址地址', trigger: 'blur' },
        ],
        accountName: [
          { required: true, message: '请输入账号', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ],
        currentState: [
          { required: true, message: '请输入状态', trigger: 'blur' },
        ],
        startTime: [
          { required: true, message: '请选择开始时间', trigger: 'change' },
        ],
        endTime: [
          { required: true, message: '请选择结束时间', trigger: 'change' },
        ],
        reminderTime: [
          { required: true, message: '请选择提醒时间', trigger: 'change' },
        ],
        msgReminderType: [
          { required: true, message: '请选择消息类型', trigger: 'change' },
        ],
        reminderTemplate: [
          { required: true, message: '请选择消息模板', trigger: 'change' },
        ],
      },
      listQuery: {
        pageIndex: 1,
        pageSize: 10,
        name: undefined
      },
      total: 0,
      list: null,
      listLoading: true,
      selRow: {},
      allInvoices: {},
      allCurrentState:{},
      allMsgReminderType:{},
      allReminderTemplate:{},
      sort:{prop: 'name', order: 'descending'}
    }
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      this.fetchData()
    },
    fetchData() {
      this.listLoading = true
      getList(this.listQuery).then(data => {
        this.list = data.data
        this.listLoading = false
        this.total = data.totalCount
      })
    },
    search() {
      this.fetchData()
    },
    reset() {
      this.listQuery.name = ''
      this.fetchData()
    },
    handleFilter() {
      this.getList()
    },
    fetchNext() {
      this.listQuery.pageIndex = this.listQuery.pageIndex + 1
      this.fetchData()
    },
    fetchPrev() {
      this.listQuery.pageIndex = this.listQuery.pageIndex - 1
      this.fetchData()
    },
    fetchPage(pageIndex) {
      this.listQuery.pageIndex = pageIndex
      this.fetchData()
    },
    changeSize(pageSize) {
      this.listQuery.pageSize = pageSize
      this.fetchData()
    },
    handleCurrentChange(currentRow, oldCurrentRow) {
      this.selRow = currentRow
    },
    initGetUserStates(){
      getUserStates({dictName:"UserStates"}).then(data=>{
        this.allCurrentState=data
      })
    },
    initReminderTemplate(){
      getWeChatAll().then(data=>{
        
        this.allReminderTemplate=data
      })
    },
    initDingDingAll(){
      getDingDingAll().then(res=>{
        this.allReminderTemplate=res
      })
    },
    resetForm() {
      this.form = {
        id: '',
        name: '',
        websiteUrl: '',
        accountName: '',
        password: '',
        currentState: '',
        description: '',
        startTime:'',
        endTime:'',
        reminderTime:'',
        msgReminderType:'',
        reminderTemplate:'',
        isReminder:false,
        disabledPwd:true
      }
    },

    add() {
      this.resetForm()
      this.initGetUserStates()
      //this.initReminderTemplate()
      this.form.disabledPwd=false
      this.formTitle = '添加'
      this.formVisible = true
      this.isAdd = true
    },
    timeFormat() {
      //日期格式化
      var time = new Date()
      var year = time.getFullYear()
      var month = time.getMonth() + 1
      var date = time.getDate()
      var hours = time.getHours()
      var minutes = time.getMinutes()
      var seconds = time.getSeconds()
      function add0(m) {
        return m < 10 ? '0' + m : m
      }
      var startTime = add0(year) + '-' + add0(month) + '-' + add0(date)+' '+add0(hours)+':'+add0(minutes)+':'+add0(seconds)
      return startTime
    },
    save() {
      if(this.form.reminderTime>this.form.startTime&&this.form.reminderTime<this.form.endTime){
       
        // if(this.form.reminderTime<this.timeFormat()){
        //   this.$message({
        //     message: '提醒时间需要大于当前时间',
        //     type: 'warning'
        //   })
        //   return false
        // }else{
          
        // }
        this.$refs['form'].validate((valid) => {
          if (valid) {
            save({
              id: this.form.id,
              name: this.form.name,
              websiteUrl: this.form.websiteUrl,
              accountName: this.form.accountName,
              password: this.form.password,
              currentState: this.form.currentState,
              startTime:this.form.startTime,
              endTime:this.form.endTime,
              reminderTime:this.form.reminderTime,
              msgReminderType:this.form.msgReminderType,
              reminderTemplate:this.form.reminderTemplate,
              isReminder:false,
              description: this.form.description,
            }).then(response => {
              this.$message({
                message: '提交成功',
                type: 'success'
              })
              this.fetchData()
              this.formVisible = false
            })
          } else {
            console.log('error submit!!')
            return false
          }
        })
        
      }else{
        this.$message({
          message: '提醒时间只能大于开始时间小于结束时间',
          type: 'warning'
        })
        return false
      }
      
    },
    checkSel() {
      if (this.selRow && this.selRow.id) {
        return true
      }
      this.$message({
        message: '请选中操作项',
        type: 'warning'
      })
      return false
    },
    edit(row) {
      console.log(row)
      this.resetForm()
      //this.initReminderTemplate()
      this.handleCurrentChange(row)
     // this.initMsgReminderType()
      this. rules= {
        name: [
          { required: true, message: '请输入工作项', trigger: 'blur' },
        ],
        websiteUrl: [
          { required: true, message: '请输入网址地址', trigger: 'blur' },
        ],
        accountName: [
          { required: true, message: '请输入账号', trigger: 'blur' },
        ],
        currentState: [
          { required: true, message: '请输入状态', trigger: 'blur' },
        ],
        startTime: [
          { required: true, message: '请选择开始时间', trigger: 'change' },
        ],
        endTime: [
          { required: true, message: '请选择结束时间', trigger: 'change' },
        ],
        reminderTime: [
          { required: true, message: '请选择提醒时间', trigger: 'change' },
        ],
        msgReminderType: [
          { required: true, message: '请选择消息类型', trigger: 'change' },
        ],
        reminderTemplate: [
          { required: true, message: '请选择消息模板', trigger: 'change' },
        ],
      }
      if (this.checkSel()) {
        this.isAdd = false
        this.form = this.selRow
        // getMsgReminder().then(data=>{
        
        //   this.allMsgReminderType=data.data
        //   this.allMsgReminderType.forEach(element => {
        //     if(element.id==this.form.msgReminderType){
        //       this.allReminderTemplate=element.templateChildren
        //     }
        //   })
        // })
        if(row.msgReminderType=="企业微信"){
          this.initReminderTemplate()
        }
        else if(row.msgReminderType=="钉钉"){
          this.initDingDingAll()
        }
        this.formTitle = '修改账号'
        //this.form.disabledPwd=true
        this.formVisible = true
      }
    },
    remove(row) {
      this.handleCurrentChange(row)
      if (this.checkSel()) {
        const id = this.selRow.id
        this.$confirm('确定删除该记录?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          remove(id).then(response => {
            this.$message({
              message: '提交成功',
              type: 'success'
            })
            this.fetchData()
          }).catch(err => {
            this.$notify.error({
              title: '错误',
              message: err
            })
          })
        }).catch(() => {
        })
      }
    },
    selectMsgReminderType(res){
      if(res=="企业微信"){
        this.initReminderTemplate()
      }
      else if(res=="钉钉"){
        this.initDingDingAll()
      }
    }
    // selectReminderTemplate(id){
    //   //console.log(this.allMsgReminderType)
    //   this.allMsgReminderType.forEach(element => {
    //     if(element.id==id){
    //       this.allReminderTemplate=element.templateChildren
    //     }
    //   })
    // }
  }
}
